/**
 * 处理例句(对应单词变色)
 * @param record
 */
export function handleExampleSentence(record) {
	let name = record.name || "";
	// 时态单词list(string[])
	const englishTenseWordList = record.englishTense?.map(obj => obj.words)?.flat()?.map(obj => obj.name) || []
	if (name) {
		record.exampleSentence.forEach((data) => {
			if (record.languageType === 2) {
				//日语匹配
				data.example = data.example?.replace(name, `<b style="color: #5480F8;font-weight: 400;">${name}</b>`)
			} else {
				//英语匹配
				const name2 = name.trim().toLowerCase()
				// 拆分例句为数组
				let examples = data.example?.split(" ") || [];
				
				// examples.forEach((val) => {
				// 	val = val.replace(/\<*((?!\<).)*\>/g, "");
				// 	let val2 = val.trim().toLowerCase().replace(/[^a-z]/g, '')
				// 	if (name2 === val2) {
				// 		// 动态正则表达式- 单词边界+单词
				// 		let reg = new RegExp(`\\b${val}`)
				// 		if (name2.length <=2 ) {
				// 			reg = new RegExp(`\\b${val}\\b`)
				// 		}
				// 		data.example = data.example.replace(reg, `<b style="color: #5480F8;font-weight: 400;">${val}</b>`)
				// 	} else if (val2.startsWith(name2) || englishTenseWordList.includes(val2)) {
				// 		if (name2.length > 1) {
				// 			data.example = data.example.replace(val, `<b style="color: #5480F8;font-weight: 400;">${val}</b>`)
				// 		}
				// 	} 
				// });
				
				for (var i = 0; i < examples.length; i++) {
					let val=examples[i]=examples[i].replace(/\<*((?!\<).)*\>/g, "");
					let val2 = examples[i].trim().toLowerCase().replace(/[^a-z]/g, '')
					if (name2 === val2) {
						// console.log('name2 === val2=', name2, val2, val)
						// 动态正则表达式- 单词边界+单词
						// let reg = new RegExp(`\\b${name2}\\b`,'i')
						if (name2.length <=2 ) {
							let reg = new RegExp(`\\b${val}\\b`)
							data.example = data.example.replace(reg, `<b style="color: #5480F8;font-weight: 400;">${val}</b>`)
						}else {
							data.example = data.example.replace(val, `<b style="color: #5480F8;font-weight: 400;">${val}</b>`)
						}
						
						break;
					} else if (val2.startsWith(name2) || englishTenseWordList.includes(val2)) {
						// console.log('val2.startsWith(name2)=', name2, val)
						if (name2.length > 1) {
							data.example = data.example.replace(val, `<b style="color: #5480F8;font-weight: 400;">${val}</b>`)
						}
					} else if(name2.length>3 && val2.startsWith(name2.substr(0,3))) {
						data.example = data.example.replace(val, `<b style="color: #5480F8;font-weight: 400;">${val}</b>`)
					}
				}
			}
		})
	}
	return record
}

function handler2() {
	const englishTenseWordList = []
	var data = {
		example: "<p>I'd also like a chance to advance .</p>"
	}
	var data = {
		example: "<p>We have to discover his plans and act accordingly.</p>"
	}
	//英语匹配
	const name2 = 'accordingly'.trim().toLowerCase()
	// 拆分例句为数组
	var examples = data.example?.split(" ") || [];
	console.log('examples=', examples)
	examples.forEach((val) => {
		val = val.replace(/\<*((?!\<).)*\>/g, "");
		let val2 = val.trim().toLowerCase().replace(/[^a-z]/g, '')
		if (name2 === val2) {
			console.log('name2 === val2=', name2, val)
			// 动态正则表达式- 单词边界+单词
			let reg = new RegExp(`\\b${val}\\b`)
			data.example = data.example.replace(reg, `<b style="color: #5480F8;font-weight: 400;">${val}</b>`)
			console.log('data.example=', data.example)
		} else if (val2.startsWith(name2) || englishTenseWordList.includes(val2)) {
			console.log('val2.startsWith(name2)=', name2, val)
			if (name2.length > 1) {
				data.example = data.example.replace(val, `<b style="color: #5480F8;font-weight: 400;">${val}</b>`)
			}
		}
	});
	for (var i = 0; i < examples.length; i++) {
		let val=examples[i]=examples[i].replace(/\<*((?!\<).)*\>/g, "");
		let val2 = examples[i].trim().toLowerCase().replace(/[^a-z]/g, '')
		if (name2 === val2) {
			console.log('name2 === val2=', name2, val)
			// 动态正则表达式- 单词边界+单词
			let reg = new RegExp(`\\b${val}\\b`)
			data.example = data.example.replace(reg, `<b style="color: #5480F8;font-weight: 400;">${val}</b>`)
			break;
		} else if (val2.startsWith(name2) || englishTenseWordList.includes(val2)) {
			console.log('val2.startsWith(name2)=', name2, val)
			if (name2.length > 1) {
				data.example = data.example.replace(val, `<b style="color: #5480F8;font-weight: 400;">${val}</b>`)
			}
		}
	}
	console.log(data)
}